//
//  WBEmoticonKeyboard.swift
//  weibo
//
//  Created by  郭航 on 16/7/4.
//  Copyright © 2016年 郭航. All rights reserved.
//

import UIKit

class WBEmoticonKeyboard: UIView {
    
    private lazy var pageView: WBEmoticonPageView = WBEmoticonPageView()
    
    private lazy var pageControl: UIPageControl = {
        let page = UIPageControl()
        
        page.numberOfPages = WBEmoticonsBundle.shareEmoticonsBundle.emoticons[0].count
        page.currentPage = 0
        
        page.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        page.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        return page
    }()
    
    private lazy var tabbarTool: WBEmoticonTabberTool =  WBEmoticonTabberTool(frame: CGRectZero)
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 216))
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        addSubview(tabbarTool)
        tabbarTool.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        
        addSubview(pageView)
        
        pageView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(tabbarTool.snp_top)
        }
        
        pageView.delegate = self
        
        addSubview(pageControl)
        
        pageControl.snp_makeConstraints { (make) in
            make.centerX.equalTo(pageView)
            make.bottom.equalTo(pageView)
        }
        
        tabbarTool.callBack = { [weak self] (tabbarToolType) -> Void in
            self?.pageView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: tabbarToolType.rawValue - 1), atScrollPosition: .Left, animated: false)
            self?.pageControl.numberOfPages = WBEmoticonsBundle.shareEmoticonsBundle.emoticons[tabbarToolType.rawValue - 1].count
            self?.pageControl.currentPage = 0
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension WBEmoticonKeyboard: UICollectionViewDelegate {
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let centerX = scrollView.contentOffset.x + SCREENW / 2
        
        let collectionView = scrollView as! UICollectionView
        
        let cells = collectionView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        if cells.count > 1 {
            var indexPath: NSIndexPath?
            
            let cell1 = cells.first!
            let cell2 = cells.last!
            
            if cell1.frame.contains(CGPoint(x: centerX, y: 0)) {
                indexPath = collectionView.indexPathForCell(cell1)
            } else {
                indexPath = collectionView.indexPathForCell(cell2)
            }
            
            pageControl.numberOfPages = WBEmoticonsBundle.shareEmoticonsBundle.emoticons[indexPath!.section].count
            pageControl.currentPage = indexPath!.item
            
            tabbarTool.setSelectButton(WBEmoticonTabberToolType(rawValue: (indexPath?.section)! + 1)!)
        }
    }
    
}
